package com.just;

import com.just.relalm.CustomerMd5Realm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.StringUtils;

public class TestCustomerMd5Realm {
    public static void main(String[] args) {
        // 1. 创建securityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        // 2. 注入Realm
        HashedCredentialsMatcher cm = new HashedCredentialsMatcher();
        cm.setHashAlgorithmName("md5");
        cm.setHashIterations(1024);
        CustomerMd5Realm realm = new CustomerMd5Realm();
        realm.setCredentialsMatcher(cm);
        securityManager.setRealm(realm);
        // 3. 工具类设置manager
        SecurityUtils.setSecurityManager(securityManager);
        // 4. 获取subject
        Subject subject = SecurityUtils.getSubject();
        // 5. 获取token
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123456");
        try {
            subject.login(token);
            System.out.println("登录成功");
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户错误");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");
        }

    }
}
